################################################################
# Replication file for: "Does Process Matter? Direct Democracy #
# and Citizens' Perceptions of Laws"                           #
#                                                              #
# Figures 2A, 2B, 4A, and 4B                                   #
#                                                              #
# Christina Ladam                                              #
# University of Colorado Boulder                               #
# christina.ladam@colorado.edu                                 #
#                                                              #
# Last updated: May 22, 2019                                   #
################################################################

### Packages ###
library(foreign)
library(ggplot2)
library(extrafont)
library(ggsignif)
library(cowplot)

### Data preparation ###
study1<-read.csv("study1.csv")
study2<-read.csv("study2.csv")


### Figure 2A ##

# Model, Fairness of Law Passage Process, Study 1
m.fair1 <- lm(fairpassage ~ -1 + t1 + t2 + t3 + t4, data=study1)

# Expected values for Same-sex Marriage Conditions, Fairness of Law Passage Process
d.fair1ss <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.fair1)[1], coef(m.fair1)[2]), lo = c(confint(m.fair1)[1, 1], confint(m.fair1)[2, 1]), hi = c(confint(m.fair1)[1, 2], confint(m.fair1)[2, 2]))

# Expected values for Personnel Guidelines Conditions, Fairness of Law Passage Process
d.fair1pg <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.fair1)[3], coef(m.fair1)[4]), lo = c(confint(m.fair1)[3, 1], confint(m.fair1)[4, 1]), hi = c(confint(m.fair1)[3, 2], confint(m.fair1)[4, 2]))

# Plotting Same-Sex Marriage Conditions, Fairness of Law Passage Process
theme_set(theme_gray(base_size = 12))
fair1ss<-ggplot(d.fair1ss, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Same-Sex Marriage") +
  ylab("Means") +
  ylim(0,100) +
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank())

# Plotting Personnel Guidelines Conditions,Fairness of Law Passage Process
fair1pg<-ggplot(d.fair1pg, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Personnel Guidelines") +
  ylab("") +
  ylim(0,100)+
  geom_signif(comparisons = list(c("Ballot Initiative", "State Legislature")), 
              map_signif_level=F, annotations="p<0.05", y_position=75,family="Times New Roman")+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank(), legend.position="")

# Combining Plots for Same-sex Marriage and Personnel Guidelines Conditions, Fairness of Law Passage Process
figure2fair <- plot_grid(fair1ss+theme(legend.position=""), 
                      fair1pg+theme(legend.position=""), 
                      align = 'vh',
                      hjust = -1,
                      nrow = 1,
                      labels="")

legend.fair1<-get_legend(fair1ss)
grDevices::pdf(NULL)
figure2a<-plot_grid(legend.fair1, figure2fair,  ncol = 1, nrow=2,  rel_heights = c(.2, 1))
grDevices::dev.off()
ggsave("figure2a.pdf", plot=figure2a, width=6.5,height=5, device=cairo_pdf)


### Figure 2B ###

# Model, Support for Law, Study 1
m.support1 <- lm(supportlaw ~ -1 + t1 + t2 + t3 + t4, data=study1)

# Expected Values for Same-Sex Marriage Conditions, Support for Law
d.support1ss <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.support1)[1], coef(m.support1)[2]), lo = c(confint(m.support1)[1, 1], confint(m.support1)[2, 1]), hi = c(confint(m.support1)[1, 2], confint(m.support1)[2, 2]))

# Expected Values for Personnel Guidelines Conditions, Support for Law
d.support1pg <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.support1)[3], coef(m.support1)[4]), lo = c(confint(m.support1)[3, 1], confint(m.support1)[4, 1]), hi = c(confint(m.support1)[3, 2], confint(m.support1)[4, 2]))

# Plotting Same-sex Marriage Conditions, Support for Law
support1ss<-ggplot(d.support1ss, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Same-Sex Marriage") +
  ylab("Means") +
  ylim(0,1) +
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank())

# Plotting Personnel Guidelines Conditions, Support for Law
support1pg<-ggplot(d.support1pg, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Personnel Guidelines") +
  ylab("") +
  ylim(0,1)+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank(), legend.position="")

# Combining Plots for Same-sex Marriage and Personnel Guidelines Conditions, Support for Law
figure2support <- plot_grid(support1ss+theme(legend.position=""), 
                         support1pg+theme(legend.position=""), 
                         align = 'vh',
                         hjust = -1,
                         nrow = 1,
                         labels="")

legend.support1<-get_legend(support1ss)
grDevices::pdf(NULL)
figure2b<-plot_grid(legend.support1, figure2support,  ncol = 1, nrow=2,  rel_heights = c(.2, 1))
grDevices::dev.off()
ggsave("figure2b.pdf", plot=figure2b, width=6.5,height=5, device=cairo_pdf)


### Figure 4A ###

# Model, Fairness of Law Passage Process, Study 2
m.fair2 <- lm(fairpassage ~ -1 + t1 + t2 + t3 + t4, data = study2)

# Expected Values for Marijuana Decriminalization Conditions, Fairness of Law Passage Process
d.fair2md <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.fair2)[1], coef(m.fair2)[2]), lo = c(confint(m.fair2)[1, 1], confint(m.fair2)[2, 1]), hi = c(confint(m.fair2)[1, 2], confint(m.fair2)[2, 2]))

# Expected Values for Auto Insurance Conditions, Fairness of Law Passage Process
d.fair2ai <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.fair2)[3], coef(m.fair2)[4]), lo = c(confint(m.fair2)[3, 1], confint(m.fair2)[4, 1]), hi = c(confint(m.fair2)[3, 2], confint(m.fair2)[4, 2]))

# Plotting Marijuana Decriminalization Conditions, Fairness of Law Passage Process
fair2md<-ggplot(d.fair2md, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Marijuana Decriminalization") +
  ylab("Means") +
  ylim(0,100)+
  geom_signif(comparisons = list(c("Ballot Initiative", "State Legislature")), 
              map_signif_level=F, annotations="p<0.01", y_position=87.5,family="Times New Roman")+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank())

# Plotting Auto Insurance Conditions, Fairness of Law Passage Process
fair2ai<-ggplot(d.fair2ai, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Auto Insurance") +
  ylab("") +
  ylim(0,100)+
  geom_signif(comparisons = list(c("Ballot Initiative", "State Legislature")), 
              map_signif_level=F, annotations="p<0.01", y_position=78,family="Times New Roman")+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank(), legend.position="")

# Combining Plots for Marijuana Decriminalization and Auto Insurance Conditions, Fairness of Law Passage Process
figure4fair <- plot_grid(fair2md+theme(legend.position=""), 
                         fair2ai+theme(legend.position=""), 
                         align = 'vh',
                         hjust = -1,
                         nrow = 1,
                         labels="")

legend.fair2<-get_legend(fair2md)
grDevices::pdf(NULL)
figure4a<-plot_grid(legend.fair2, figure4fair,  ncol = 1, nrow=2,  rel_heights = c(.2, 1))
grDevices::dev.off()
ggsave("figure4a.pdf", plot=figure4a, width=6.5,height=5, device=cairo_pdf)


###Figure 4B ###

# Model, Support for Law, Study 2
m.support2 <- lm(supportlaw ~ -1 + t1 + t2 + t3 + t4, data = study2)

# Expected Values for Marijuana Decriminalization Conditions, Support for Law
d.support2md <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.support2)[1], coef(m.support2)[2]), lo = c(confint(m.support2)[1, 1], confint(m.support2)[2, 1]), hi = c(confint(m.support2)[1, 2], confint(m.support2)[2, 2]))

# Expected Values for Auto Insurance Conditions, Support for Law
d.support2ai <- data.frame(Condition = c("Ballot Initiative", "State Legislature"), means = c(coef(m.support2)[3], coef(m.support2)[4]), lo = c(confint(m.support2)[3, 1], confint(m.support2)[4, 1]), hi = c(confint(m.support2)[3, 2], confint(m.support2)[4, 2]))

# Plotting Marijuana Decriminalization Conditions, Support for Law
support2md<-ggplot(d.support2md, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Marijuana Decriminalization") +
  ylab("Means") +
  ylim(0,1)+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank())

# Plotting Auto Insurance Conditions, Support for Law
support2ai<-ggplot(d.support2ai, aes(x = Condition, y = means, fill = Condition)) +
  geom_bar(stat = "identity", colour="black") + 
  geom_errorbar(aes(ymin = lo, ymax = hi), size = .4, width = .2) +
  scale_fill_grey(start=1, end=0.5) +
  xlab("Auto Insurance") +
  ylab("") +
  ylim(0,1)+
  geom_signif(comparisons = list(c("Ballot Initiative", "State Legislature")), 
              map_signif_level=F, annotations="p<0.01", y_position=.83,family="Times New Roman")+
  theme(text=element_text(size=12, family="Times New Roman"), axis.text.x=element_blank(), axis.ticks=element_blank(), legend.position="")

# Combining Marijuana Decriminalization and Auto Insurance Conditions, Support for Law
figure4support <- plot_grid(support2md+theme(legend.position=""), 
                         support2ai+theme(legend.position=""), 
                         align = 'vh',
                         hjust = -1,
                         nrow = 1,
                         labels="")

legend.support2<-get_legend(support2md)
grDevices::pdf(NULL)
figure4b<-plot_grid(legend.support2, figure4support,  ncol = 1, nrow=2,  rel_heights = c(.2, 1))
grDevices::dev.off()
ggsave("figure4b.pdf", plot=figure4b, width=6.5,height=5, device=cairo_pdf)


